package com.mall.b2c.plugin.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.mall.b2c.bean.GoodsSort;
import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.b2c.service.GoodsCategoryService;
import com.mall.b2c.util.DictionaryUtils;
import com.mall.common.hibernate.Finder;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.util.UrlUtils;
/**
 * 商品排序筛选器
 * @author wangzx
 *
 */
public class SortSearchFilter extends AutoRegisterPlugin implements IGoodsSearchFilter{
	@Resource GoodsCategoryService goodsCategoryService;
	
	@Override
	public void filter(Finder finder, GoodsCategoryDomain cat, String urlFragment) {
		String order = " (case WHEN g.goods.stock is null THEN 0 WHEN g.goods.stock > 0 THEN 1 ELSE 0 END) desc";
		if(StringUtils.isNotBlank(urlFragment)) {
			for(GoodsSort goodsSort : GoodsSort.values()) {
				if(urlFragment.equals(String.valueOf(goodsSort.ordinal()))) {
					order = order + ", g.goods." + goodsSort.getSortStr();
					break;
				}
			}
		} else {
			order = order + ", g.goods.priority desc";
		}
		order += ", g.goods.name desc";
		finder.append(" order by ").append(order);
	}

	@Override
	public List<SearchSelector> createSelectorList(GoodsCategoryDomain cat,
			String url, String urlFragment, Map<String, String> paramMap,
			String hql, Finder finder) {
		List<SearchSelector> searchSelectors = new ArrayList<SearchSelector>();
		SearchSelector searchSelector = null;
		for(GoodsSort goodsSort : GoodsSort.values()) {
			if(String.valueOf(GoodsSort.PriceAsc.ordinal()).equals(urlFragment) && goodsSort == GoodsSort.PriceAsc) {
				continue;
			}
			if(!String.valueOf(GoodsSort.PriceAsc.ordinal()).equals(urlFragment) && goodsSort == GoodsSort.PriceDesc) {
				continue;
			}
			searchSelector = new SearchSelector();
			searchSelector.setName(DictionaryUtils.getEnumName(goodsSort) + goodsSort.name());
			if(StringUtils.isBlank(urlFragment)) {
				 if(goodsSort == GoodsSort.TopDesc) {
					 searchSelector.setSelected(Boolean.TRUE);
				 }
			} else if(urlFragment.equals(String.valueOf(goodsSort.ordinal()))) {
				searchSelector.setSelected(Boolean.TRUE);
			} else if(urlFragment.equals(String.valueOf(GoodsSort.PriceAsc.ordinal())) && goodsSort == GoodsSort.PriceDesc) {
				searchSelector.setSelected(Boolean.TRUE);
			} else if(urlFragment.equals(String.valueOf(GoodsSort.PriceDesc.ordinal())) && goodsSort == GoodsSort.PriceAsc) {
				searchSelector.setSelected(Boolean.TRUE);
			} else {
				searchSelector.setSelected(Boolean.FALSE);
			}
			searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), String.valueOf(goodsSort.ordinal()), false));
			searchSelectors.add(searchSelector);
		}
		return searchSelectors;
	}

	@Override
	public String getFilterId() {
		return "sort";
	}

	@Override
	public String getSysId() {
		return "sortList";
	}

	@Override
	public String getType() {
		return "goodsSearchFilter";
	}

	@Override
	public String getId() {
		return "sortSearchFilter";
	}

	@Override
	public String getName() {
		return "商品排序筛选器";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "wangzx";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}
}
